<?php
namespace app\admin\controller;
use app\system\model\Menu;

use think\Controller;
use think\Db;

class Base extends Controller
{
    static $admin_info = [];
    public function __construct($module = '')
    {
        parent::__construct();

        $admin_info = session('admin_info');

        if(empty($admin_info)){
            $this->error('尚未登录','index.php/admin/index/index');
        }else{
            //传入控制器
            self::$admin_info = $admin_info;
            self::loadModuleMenu($module);
            //当前登录信息 传入模板
            $this->assign('admin_info',$admin_info);
        }
    }

    protected function loadModuleMenu($module = ''){
        $data = self::$admin_info;
        $result['left_menu']=[];

        #获取控制器
        $c = request()->controller();
        $a = request()->action();
        $nowac = $c."-".$a;

        //获取当前用户对应的角色权限
        $manage_info = Db::name('member')
            ->find($data['id']);

        $role_info = Db::name('role')
            ->find($manage_info['identity']);
        $auth_ac = $role_info['role_auth_ac'];

        //有些页面不设访问限制,如登录页面
        $alloac = "Admin-myArticleList,Admin-insert,admin-articleUpdate,admin-cp_update,Admin-articleList";

        //判断用户当前访问的控制器和方法是否在权限列表里，访问的页面是否属于无限制的页面
        //且admin（超级管理员）不受访问限制
        if (stripos ($auth_ac,$nowac)===false
            && stripos ($alloac,$nowac)===false
            && $data['id']!==1 ){
            exit('没有访问权限');
        }

        if ($data['id']===1){
            $auth_infoA = Db::name('auth')
                ->where('auth_level=0 and status=1 and del=1')
                ->order(['sort'=>SORT_DESC])
                ->select();
            $auth_infoB = Db::name('auth')
                ->where('auth_level=1 and status=1 and del=1')
                ->order(['sort'=>SORT_DESC])
                ->select();
        }else{
            $auth_infoA = Db::name('auth')
                ->where(["auth_level"=>0,'auth_id'=>['in',$role_info['role_auth_ids']],'status'=>1,'del'=>1])
                ->order(['sort'=>SORT_DESC])
                ->select();
            $auth_infoB = Db::name('auth')
                ->where(["auth_level"=>1,'auth_id'=>['in',$role_info['role_auth_ids']],'status'=>1,'del'=>1])
                ->order(['sort'=>SORT_DESC])
                ->select();
        }

        $auth_name = Db::name('auth')->where(['auth_c'=>$c,'auth_a'=>$a])->value('auth_name');

        $this->assign('auth_infoA',$auth_infoA);
        $this->assign('auth_infoB',$auth_infoB);
        $this->assign('auth_name',$auth_name);
    }

}
